##Controlling Graph Size in RMarkdown

#Loading Library
library(tidyverse)
## ── Attaching packages ────────────────────────────────────────────────────── tidyverse 1.3.0 ──
## ✓ ggplot2 3.2.1     ✓ purrr   0.3.3
## ✓ tibble  2.1.3     ✓ dplyr   0.8.4
## ✓ tidyr   1.0.2     ✓ stringr 1.4.0
## ✓ readr   1.3.1     ✓ forcats 0.4.0
## ── Conflicts ───────────────────────────────────────────────────────── tidyverse_conflicts() ──
## x dplyr::filter() masks stats::filter()
## x dplyr::lag()    masks stats::lag()
#Loading Files 
SNPs<- read.table("23andMe_complete.txt", header = TRUE, sep = "\t")
#to adjust figure size {r, fig.width = 6, fig.height = 6}
SNPs$chromosome = ordered(SNPs$chromosome, levels=c(seq(1, 22), "X", "Y", "MT"))
ggplot(data = SNPs) + 
  geom_bar(mapping = aes(x = genotype, fill = chromosome)) + 
  coord_polar() +
  ggtitle("Total SNPs for each genotype") +
  ylab("Total number of SNPs") +
  xlab("Genotype")

##Graphic output

#For a cleaner looking graph
SNPs$chromosome = ordered(SNPs$chromosome, levels=c(seq(1, 22), "X", "Y", "MT"))
#Plot graph to a pdf outputfile
pdf("SNP_example_plot.pdf", width=6, height=3)
ggplot(data = SNPs) + 
  geom_bar(mapping = aes(x = chromosome, fill = genotype))
dev.off()
## quartz_off_screen 
##                 2
#Plot graph to a png outputfile
ppi <- 300
png("SNP_example_plot.png", width=6*ppi, height=6*ppi, res=ppi)
ggplot(data = SNPs) + 
  geom_bar(mapping = aes(x = chromosome, fill = genotype))
dev.off()
## quartz_off_screen 
##                 2

#RMarkdown Loading Images

Genotype counts per chromosome

img src = “SNP_example_plot.png” alt=“Genotype counts per chromosome” style=“width: 600px;”

#Interactive graphs and tables in RMakrdown reports

#Version 1.1
library(plotly)
## 
## Attaching package: 'plotly'
## The following object is masked from 'package:ggplot2':
## 
##     last_plot
## The following object is masked from 'package:stats':
## 
##     filter
## The following object is masked from 'package:graphics':
## 
##     layout
p <- ggplot(data = iris, aes(x = Sepal.Length, y = Sepal.Width, color = Species)) + 
  geom_point()
ggplotly(p)
#Version 2
library(plotly)
ggplotly(
  ggplot(data = iris, aes(x = Sepal.Length, y = Sepal.Width, color = Species)) + 
    geom_point()
)
#DT Package
library(DT)
datatable(iris)

Exercise 1

library(tidyverse)
SNPs<- read.table("23andMe_complete.txt", header = TRUE, sep = "\t")
SNPs$chromosome = ordered(SNPs$chromosome, levels=c(seq(1, 22), "X", "Y", "MT"))
ggplot(data = SNPs) +
  geom_bar(mapping = aes(x = chromosome),fill="blue") +
  ggtitle("Total SNPs per chromosome") +
  ylab("Total number of SNPs") +
  xlab("Chromosome")

Exercise 2

SNPs$chromosome = ordered(SNPs$chromosome, levels=c(seq(1, 22), "X", "Y", "MT"))
genocolor<-c("--"="royalblue4","A"="cornflowerblue","AA"="royalblue4","AC"="royalblue4","AG"="royalblue4","AT"="royalblue4","C"="cornflowerblue","CC"="royalblue4","CG"="royalblue4","CT"="royalblue4","D"="cornsilk1","DD"="cornsilk1","DI"="cornsilk1","G"="cornflowerblue","GG"="royalblue4","GT"="royalblue4","I"="cornsilk1","II"="cornsilk1","T"="cornflowerblue","TT"="royalblue4")
ggplot(data = SNPs) + 
  geom_bar(mapping = aes(x = chromosome, fill = genotype)) + 
  ggtitle("Total SNPs per chromsome (by genotype)") +
  ylab("Total number of SNPs") +
  xlab("Chromosome") +
  scale_fill_manual (values=genocolor)

Exercise 3

ppi <- 300
png("SNP_exercise3_plot.png", width=6*ppi, height=6*ppi, res=ppi)
ggplot(data = SNPs) +
  geom_bar(mapping = aes(chromosome, fill = genotype), position = "dodge")

total SNPs per chromosome (by genotype_)

Exercise 4

ppi <- 300
png("SNP_exercise4_plot.png", width=8*ppi, height=6*ppi, res=ppi)
ggplot(data = SNPs) +
  geom_bar(mapping = aes(chromosome, fill = genotype), position = "dodge") +
  facet_wrap(~genotype) +
  ggtitle("Number of SNPs per Genotype per Chromosome") +
  ylab("Total Count of SNPs per Genotype") +
  xlab("Chromosome Number")

Number of SNPs per Genotype per Chromsome

Exercise 5

library(plotly)
t <- ggplot(data = SNPs) +
  geom_bar(mapping = aes(chromosome, fill = genotype), position = "dodge") +
  facet_wrap(~genotype) +
  ggtitle("Number of SNPs per Genotype per Chromosome") +
  ylab("Total Count of SNPs per Genotype") +
  xlab("Chromosome Number")
ggplotly(t)

Exercise 6

library(plotly)
SNPs <- read.table("23andMe_complete.txt", header = TRUE, sep = "\t")
SNPs$chromosome = levels=c(seq("Y"))
ChromY<-ggplot(SNPs) +
  geom_bar(mapping = aes(SNPs$chromosome, fill = genotype), position = "dodge") +
  xlab("Chromosome Y Genotypes") +
  ylab("Total Number of SNPs")
ggplotly(ChromY)